package com.hucii.app.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hucii.app.R;
import com.hucii.app.view.GridViewPicItemView;

public class GridViewPicAdapter extends BaseAdapter {

	private Context mContext;
	private List<TableRow> mTable;

	public GridViewPicAdapter(Context context, List<TableRow> table) {
		this.mContext = context;
		this.mTable = table;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mTable.size();
	}

	public TableRow getItem(int position) {
		// TODO Auto-generated method stub
		return mTable.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TableRow tableRow = mTable.get(position);
		return new TableRowView(mContext, tableRow, position);
	}

	class TableRowView extends LinearLayout {
		public TableRowView(Context context, TableRow tableRow, int position) {
			super(context);
			this.setOrientation(LinearLayout.HORIZONTAL);
			for (int i = 0; i < tableRow.getSize(); i++) {
				TableCell tableCell = tableRow.getCellValue(i);
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
						tableCell.width, tableCell.height);// 按照格单元指定的大小设置空间
				layoutParams.setMargins(0, 0, 1, 1);// 预留空隙制造边框

				LayoutInflater inflater = LayoutInflater.from(context);
				View view = inflater.inflate(R.layout.main_ui_pic_item, null);
				if (position % 2 == 0) {
					view.setBackgroundColor(context.getResources().getColor(
							R.color.deep_colour));
				} else {
					view.setBackgroundColor(context.getResources().getColor(
							R.color.undertint));
				}

				GridViewPicItemView gridViewPicItemView = new GridViewPicItemView();
				gridViewPicItemView.iv_thumbnail = (ImageView) view
						.findViewById(R.id.iv_thumbnail);
				gridViewPicItemView.tv_style_id = (TextView) view
						.findViewById(R.id.tv_style_id);
				gridViewPicItemView.tv_order_nums = (TextView) view
						.findViewById(R.id.tv_order_nums);
				gridViewPicItemView.tv_type = (TextView) view
						.findViewById(R.id.tv_type);
				view.setTag(gridViewPicItemView);
				
				gridViewPicItemView.getTv_style_id()
						.setText(tableCell.mStyleId);
				gridViewPicItemView.getTv_order_nums().setText(
						tableCell.mOrderNums);
				gridViewPicItemView.getTv_type().setText(tableCell.mType);

				view.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Toast.makeText(mContext,
								"<<<---onClick --->>" + v.getTag(),
								Toast.LENGTH_SHORT).show();
					}
				});

				addView(view, layoutParams);
			}
			this.setBackgroundColor(Color.WHITE);
		}
	}

	/**
	 * TableRow 实现表格的行
	 * 
	 * @author hellogv
	 */
	static public class TableRow {
		private TableCell[] cell;

		public TableRow(TableCell[] cell) {
			this.cell = cell;
		}

		public int getSize() {
			return cell.length;
		}

		public TableCell getCellValue(int index) {
			if (index >= cell.length)
				return null;
			return cell[index];
		}
	}

	/**
	 * TableCell 实现表格的格单元
	 * 
	 * @author hellogv
	 */
	static public class TableCell {
		static public final int STRING = 0;
		static public final int IMAGE = 1;
		public Object value;
		public int width;
		public int height;
		private int type;
		public String mStyleId;
		public String mOrderNums;
		public String mType;

		public TableCell(Object value, int width, int height, int type) {
			this.value = value;
			this.width = width;
			this.height = height;
			this.type = type;
		}
	}

}
